<template>
  <div class="newaddress">
    <van-nav-bar
      :title="this.aid ? '编辑地址' : '新增地址'"
      left-text="返回"
      left-arrow
      fixed
      @click-left="back"
    />
    <van-address-edit
    :area-list="areaList"
     show-postal
      :show-delete="!!this.aid"
      :address-info="initAddress"
      show-set-default
      :area-columns-placeholder="['选择省', '选择市', '选择区/县']"
      @save="save"
      @delete="removeAddress"
    />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data';
export default {
  name: "newaddress",
  data() {
    return {
           //省市区列表数据
      areaList,
      aid: "",
      //地址初始化
      initAddress: {}
   
    };
  },
  created() {
      //截取路由aid
      this.aid = this.$route.params.aid;
      // console.log("this.aid9999",this.aid);

      //只有在编辑地址才根据aid查询地址数据
      if(this.aid){
          this.getAddressByAid(this.aid)
      }
  },
  methods: {
    //返回上一级
    back() {
      this.$router.go(-1);
    },
    //根据aid获取地址数据
    getAddressByAid(aid){
        let tokenString = sessionStorage.getItem("token");

      this.axios({
          method:'get',
          url:'/findAddressByAid',
          params:{
              tokenString,
              aid
              
          }
      }).then(res=>{
        if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)

            return;
          }
          this.$toast(res.data.msg);
          if(res.data.code === 40000){
             let data = res.data.result[0];
             data.isDefault = Boolean(data.isDefault);
             this.initAddress = data
          }
      }).catch(err=>{
          console.log(err);
      })
    },
    //新建地址
    addAddress(content){
        //复制content
      let addressData = {...content};
      addressData.isDefault = Number(addressData.isDefault);
      delete addressData.country;

      let tokenString = sessionStorage.getItem("token");

      this.axios({
          method:'post',
          url:'/addAddress',
          data:{
              tokenString,
              ...addressData
          }
      }).then(res=>{
          // console.log(res);
                    if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)

            return;
          }
          this.$toast(res.data.msg);
          if(res.data.code === 9000){
              setTimeout(()=>{
                  this.$router.push({name:'Address'})
              },2000)
          }
      }).catch(err=>{
          console.log(err);
      })
    },
    //编辑地址
    eidtAddress(content){
              //复制content
      let addressData = {...content};
      // console.log("addressData",addressData);

      addressData.isDefault = Number(addressData.isDefault);
      delete addressData.country;

       let tokenString = sessionStorage.getItem("token");

      this.axios({
          method:'post',
          url:'/editAddress',
          data:{
              tokenString,
              aid:this.aid,
              ...addressData
              
          }
      }).then(res=>{
          // console.log("编辑地址55",res);
                    if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)

            return;
          }
          this.$toast(res.data.msg);
          if(res.data.code === 30000){
              setTimeout(()=>{
                  this.$router.push({name:'Address'})
              },2000)
          }
      }).catch(err=>{
          console.log(err);
      })
    },
    //保存地址
    save(content){
        if(this.aid){
            //编辑地址
            this.eidtAddress(content);
        }else{
            //新增地址
            this.addAddress(content)
        }
    },
    //删除地址
    removeAddress(){
         let tokenString = sessionStorage.getItem("token");

      this.axios({
          method:'post',
          url:'/deleteAddress',
          data:{
              tokenString,
              aid:this.aid
              
          }
      }).then(res=>{
          // console.log(res);
                    if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)

            return;
          }
          this.$toast(res.data.msg);
          if(res.data.code === 10000){
              setTimeout(()=>{
                  this.$router.push({name:'Address'})
              },2000)
          }
      }).catch(err=>{
          console.log(err);
      })
    }
  },
};
</script>

<style lang="less" scoped>
.newaddress {
  padding-top: 46px;
}
/deep/.van-address-item{
  margin-top: 80px;
}
/deep/.van-button--danger{
   border-color: #0c34ba;
    background-color: #0c34ba;
}
</style>